/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lvyh.lightframe.job.common;

import com.alibaba.fastjson.JSON;
import com.lvyh.lightframe.job.enums.ErrorCode;

public class Result<T> {

    private long status; // 0-success ,1-failed
    private String message;
    private T result;

    public static Result SUCCESS() {
        return new Result(ErrorCode.SUCCESS.getStatus(), ErrorCode.SUCCESS.getMessage());
    }

    public static Result FAILED() {
        return new Result(ErrorCode.FAILED.getStatus(), ErrorCode.FAILED.getMessage());
    }

    public static Result create(ErrorCode code, String message) {
        return new Result(code.getStatus(), message);
    }

    public static Result create() {
        return new Result(ErrorCode.SUCCESS.getStatus(), ErrorCode.SUCCESS.getMessage());
    }

    public boolean isSuccess() {
        return ErrorCode.SUCCESS.getStatus() == status;
    }

    public Result() {
        super();
    }

    public Result(long status, String message) {
        super();
        this.status = status;
        this.message = message;
    }

    public Result(long status, String message, T result) {
        super();
        this.status = status;
        this.message = message;
        this.result = result;
    }

    public long getStatus() {
        return status;
    }

    public void setStatus(long status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    public Result setMessage(String message) {
        this.message = message;
        return this;
    }

    public Result setMsg(String message, Object... args) {
        this.message = args == null || args.length == 0 ? message : String.format(message, args);
        return this;
    }


    public T getResult() {
        return result;
    }

    public Result setResult(T result) {
        this.result = result;
        return this;
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this, true);
    }
}
